from openpyxl import Workbook
from openpyxl.drawing.image import Image


def export_images_to_excel(image_data):
    # 创建一个新的Workbook
    wb = Workbook()
    ws = wb.active
    ws.title = "Images"

    # 设置表头
    ws['A1'] = "Image Name"
    ws['B1'] = "Image URL"

    # 写入图片数据
    for index, (image_name, image_url) in enumerate(image_data, start=2):
        ws[f'A{index}'] = image_name
        ws[f'B{index}'] = image_url
        print(image_url)
        # 插入图片
        img = Image(image_url)
        img.anchor = f'C{index}'  # 设置图片插入的单元格
        ws.add_image(img)

    # 保存文件
    wb.save("images.xlsx")


if __name__ == "__main__":
    # 测试数据，假设这里是从某个数据源获取的图片信息（名称和URL）
    images = [
        ("Image1", "aaa.jpg"),
        ("Image2", "aaa.jpg"),
        ("Image2", "aaa.jpg"),
        ("Image2", "aaa.jpg"),
        ("Image3", "aaa.jpg")
    ]

    # 导出图片信息到Excel文件
    export_images_to_excel(images)
